import { Injectable } from '@angular/core';
import { ToastController,LoadingController,AlertController,ActionSheetController } from '@ionic/angular';
import { ToolService } from './tool.service';
@Injectable({
  providedIn: 'root'
})
export class PopwinService {

  constructor(private toastCtrl:ToastController,private loadingCtrl:LoadingController,private alertCtrl:AlertController,private actionSheetCtrl:ActionSheetController) { }
  /**
   * ToastController的提示
   * @param message 
   * @param position 
   * @param duration 
   */
  async toast(message:string,position?: "top" | "bottom" | "middle",duration?:number){
    if(ToolService.isEmpty(message)){
        return;
    }
    if(ToolService.isEmpty(duration)){
      duration=2000;
    }
    if(ToolService.isEmpty(position)){
      position="top";
    }
    const toast = await this.toastCtrl.create({
      message: message,
      duration: duration,
      mode:'ios',
      position:position
    });
    toast.present();
  }
  /**
   * LoadingController对loading的简单封装处理
   * @param message 
   * @param spinner 
   * @param duration 
   */
  async loading(message,spinner?:"bubbles" | "circles" | "crescent" | "dots" | "lines" | "lines-small" | null | undefined,duration?:number) {
    if(ToolService.isEmpty(message)){
      return;
    }
    if(ToolService.isEmpty(spinner)){
      spinner="circles";
    }
    if(ToolService.isEmpty(duration)){
      duration=2000;
    }
    const loading = await this.loadingCtrl.create({
      message: message,
      duration: duration,
      mode:'ios',
      spinner:spinner
    });
    await loading.present();
  }
  /**
   * 只有确定按钮的alert的弹窗
   * @param message  可以是一段html的string
   * @param header  
   * @param subHeader 
   * @param callback  确定之后的回调函数 
   */
  async alert(message?:string,header?:string,subHeader?:string,callback?: any) {
    if(ToolService.isEmpty(message)){
      message=null;
    }
    if(ToolService.isEmpty(header)){
      header=null;
    }
    if(ToolService.isEmpty(subHeader)){
      subHeader=null;
    }
    const alert = await this.alertCtrl.create({
      header: header,
      subHeader: subHeader,
      message: message,
      mode:'ios',
      buttons: [{
        text: '确定',
        handler: () => {
          if(!ToolService.isEmpty(callback)){
            callback();
          }
        }
      }]
    });
    await alert.present();
  }

  /**
   * 有确定和取消的一个按钮
   * @param message 
   * @param header 
   * @param subHeader 
   * @param callbackCancel  取消按钮的回调
   * @param callbackOK  确定按钮的回调
   */
  async alertOkAndCancel(message?:string,header?:string,subHeader?:string,callbackCancel?: any,callbackOK?: any) {
    if(ToolService.isEmpty(message)){
      message=null;
    }
    if(ToolService.isEmpty(header)){
      header=null;
    }
    if(ToolService.isEmpty(subHeader)){
      subHeader=null;
    }
    const alert = await this.alertCtrl.create({
      header: header,
      subHeader: subHeader,
      message: message,
      mode:'ios',
      buttons: [
        {
          text: '取消',
          role: 'cancel',
          handler: (blah) => { 
            if(!ToolService.isEmpty(callbackCancel)){
              callbackCancel();
            }
          }
        }, {
          text: '确定',
          handler: () => {
            if(!ToolService.isEmpty(callbackOK)){
              callbackOK();
            }
          }
        }
      ]
    });
    await alert.present();
  }
  /**
   * 显示多button的一个actionSheet的控件
   * @param buttons  buttons是一个数组且里面的数据是{ text: element.Item1, handler: () => { fun } }
   * @param title 标题
   * @param isCancel  是否有取消按钮
   * @param isCancelIcon  是否有取消按钮的icon
   */
  async actionSheet(buttons: any,title?:string,isCancel?:boolean,isCancelIcon?:boolean) {
    let tempButton=[];
    tempButton=buttons;
    if(isCancel){
      if(isCancelIcon){
        tempButton.push({ text: '取消', role: 'cancel',icon: 'close', handler: () => { } });
      }else{
        tempButton.push({ text: '取消', role: 'cancel', handler: () => { } });
      }
    }
    if(ToolService.isEmpty(title)){
      title="";
    }
    const actionSheet = await this.actionSheetCtrl.create({
      header: title,
      mode:'ios',
      buttons: tempButton
    });
    await actionSheet.present();
  }

}
